<template>
  <div>
    <home-header></home-header>
    <home-swiper :List="swiperList"></home-swiper>
    <home-category :IconList="iconList"></home-category>
    <home-recommend :RecommendList="recommendList"></home-recommend>
    <home-weekend :WeekendList="weekendList"></home-weekend>
  </div>
</template>

<script>

import HomeHeader from './components/Header.vue'
import HomeSwiper from './components/swiper.vue'
import homeCategory from './components/Category'
import HomeRecommend from './components/Recommend.vue'
import HomeWeekend from './components/Weekend.vue'

import axios from 'Axios'

import { mapState } from 'vuex';

export default {
  name: 'Home',
  data(){
    return{
      lastCity:'',
      swiperList:[],
      iconList:[],
      recommendList:[],
      weekendList:[]
    }
  },
  components:{
    HomeHeader,
    HomeSwiper,
    homeCategory,
    HomeRecommend,
    HomeWeekend
  },
  mounted(){
    this.lastCity = this.city;
    this.getHomeInfo()
    // console.log('mounted');
    
  },
  activated () {
    if (this.lastCity !== this.city) {
      this.lastCity = this.city;
      this.getHomeInfo()
    }
    // console.log('activated'); 
  },
  computed: {
    ...mapState(['city'])
  },
  methods:{
    getHomeInfo(){
      axios.get('/api/index.json?city=' + this.city)
        .then(res=>{
          // console.log(res.data);
          var res = res.data;
          // console.log(res.data);          
          if (res.ret && res.data) {
            this.swiperList = res.data.swiperList;
            this.iconList = res.data.iconList;
            this.recommendList = res.data.recommendList;
            this.weekendList = res.data.weekendList;
          }
        })
        .catch(err=>{
          console.log(err);
        })
    }
  },
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>
